
#include "Common.h"

static const char *ErrMsg[] =
{
		"No error",
		"No memory",
		"Invalid state",
		"Invalid params",
		"Timeout",
		"Internal error",
};

const char*
C_ErrMsg(C_Error_t err)
{
	if (err >= 0 && err < C_ERR_MAX)
	{
		return ErrMsg[err];
	}
	return "C_ERR_???";
}

void*
C_Malloc(size_t s)
{
	void *ptr = malloc(s);
	return ptr;
}

void
C_Free(void *ptr)
{
	if (ptr != NULL) free(ptr);
}

void
C_Get_Time(struct timespec *ts)
{
	struct timeval tv;
	if (ts == NULL)
		return;
	gettimeofday(&tv, NULL);
	ts->tv_sec = tv.tv_sec;
	ts->tv_nsec = tv.tv_usec * 1000;
}

void
C_Add_Time(struct timespec *ts, size_t ms)
{
	if (ts == NULL)
		return;

	ts->tv_sec += ms/1000;
	ts->tv_nsec += (ms - (ms/1000)*1000) * 1000000;
	if (ts->tv_nsec >= 1000000000)
	{
		ts->tv_sec++;
		ts->tv_nsec -= 1000000000;
	}
}
